package com.example.facecat.product.script;

import facecat.topin.chart.*;
import facecat.topin.core.*;
import facecat.topin.xml.*;
import facecat.topin.grid.*;

public class NFunctionEx extends CFunction {
    /*
    * 创建方法
    */
    public NFunctionEx(FCScript indicator, int id, String name, FCUIXml xml) {
        m_indicator = indicator;
        m_ID = id;
        m_name = name;
        m_xml = xml;
    }

    /*
    * 脚本
    */
    public FCScript m_indicator;

    /*
    * XML对象
    */
    public FCUIXml m_xml;

    /*
    * 方法
    */
    private static String FUNCTIONS = "GETATTRIBUTE,SETATTRIBUTE,GETSENDER,ALERT,INVALIDATE,STARTTIMER,STOPTIMER,GETTOUCHBUTTON,GETTOUCHPOINT,GETCLICKS,GETKEY,UPDATE,CALLFUNCTION,GETNEWTIMERID";
    /*
    * 前缀
    */
    private static String PREFIX = "";

    /*
    * 开始索引
    */
    private static final int STARTINDEX = 2000;

    /*
    * 计算
    */
    @Override
    public double onCalculate(CVariable var) {
        switch (var.m_functionID) {
            case STARTINDEX + 0:
            return GETATTRIBUTE(var);
        case STARTINDEX + 1:
            return SETATTRIBUTE(var);
        case STARTINDEX + 2:
            return GETSENDER(var);
        case STARTINDEX + 3:
            return ALERT(var);
        case STARTINDEX + 4:
            return INVALIDATE(var);
        case STARTINDEX + 5:
            return STARTTIMER(var);
        case STARTINDEX + 6:
            return STOPTIMER(var);
        case STARTINDEX + 7:
            return GETTOUCHBUTTON(var);
        case STARTINDEX + 8:
            return GETTOUCHPOINT(var);
        case STARTINDEX + 9:
            return GETCLICKS(var);
        case STARTINDEX + 10:
            return GETKEY(var);
        case STARTINDEX + 11:
            return UPDATE(var);
        case STARTINDEX + 12:
            return CALLFUNCTION(var);
        case STARTINDEX + 13:
            return GETNEWTIMERID(var);
            default:
                return 0;
        }
    }
    
    /*
    * 获取最新的秒表ID
    */
    public double GETNEWTIMERID(CVariable var){
        return FCView.getNewTimerID();
    }

    public double CALLFUNCTION(CVariable var){
        String name = m_indicator.getText(var.m_parameters[0]);
        FCView view = m_xml.findView(name);
        String propertyName = m_indicator.getText(var.m_parameters[1]);
        String propertyValue = m_indicator.getText(var.m_parameters[2]);
        String result = view.callFunction(propertyName, propertyValue);
        String text = "'" + result + "'";
        m_indicator.m_resultVar = new CVar();
        m_indicator.m_resultVar.m_type = 1;
        m_indicator.m_resultVar.m_str = text;
        return text.length();
    }

    /*
    * 创建指标
    */
    public static FCScript createIndicator(String script, FCUIXml xml)
    {
        FCScript newScript = new FCScript();
        FCDataTable table = new FCDataTable();
        newScript.setDataSource(table);
        int index = STARTINDEX;
        String[] functions = FUNCTIONS.split("[,]");
        int functionsSize = functions.length;
        for (int i = 0; i < functionsSize; i++)
        {
            newScript.addFunction(new NFunctionEx(newScript, index + i, functions[i], xml));
        }
        newScript.setScript(script);
        table.addColumn(0);
        table.set(0, 0, 0);
        newScript.onCalculate(0);
        return newScript;
    }
    
    /*
    * 弹出提示
    */
    private double ALERT(CVariable var) {
        return 0;
    }

    /*
    * 获取点击次数
    */
    public int GETCLICKS(CVariable var) {
        return m_xml.getEvent().getTouchInfo().m_clicks;
    }

    /*
    * 获取按键
    */
    public int GETKEY(CVariable var) {
        return (int)m_xml.getEvent().getKey();
    }

    /*
    * 获取鼠标按键
    */
    public int GETTOUCHBUTTON(CVariable var) {
        if (m_xml.getEvent().getTouchInfo().m_firstTouch) {
            return 1;
        } else if (m_xml.getEvent().getTouchInfo().m_secondTouch) {
            return 2;
        } else {
            return 0;
        }
    }

    /*
    * 获取鼠标按键
    */
    public int GETTOUCHPOINT(CVariable var) {
        FCPoint touchPoint = m_xml.getEvent().getTouchInfo().m_firstPoint;
        CVariable newVar = new CVariable(m_indicator);
        newVar.m_expression = FCTran.intToStr(touchPoint.x);
        m_indicator.setVariable(var.m_parameters[0], newVar);
        CVariable newVar2 = new CVariable(m_indicator);
        newVar2.m_expression = FCTran.intToStr(touchPoint.y);
        m_indicator.setVariable(var.m_parameters[1], newVar2);
        return 1;
    }

    /*
    * 获取属性
    */
    public double GETATTRIBUTE(CVariable var) {
        FaceCatScript fScript = (FaceCatScript)m_xml.getScript();
        if (var.m_parameters.length == 2)
        {
            String name = m_indicator.getText(var.m_parameters[0]);
            String propertyName = m_indicator.getText(var.m_parameters[1]);
            String text = "";
            if (name.length() == 0)
            {
                if (m_xml.getNative().getFocusedView() != null)
                {
                    text = m_xml.getNative().getFocusedView().getAttribute(propertyName);
                }
            }
            else
            {
                text = fScript.getAttribute(name, propertyName);
            }
            m_indicator.m_resultVar = new CVar();
            m_indicator.m_resultVar.m_type = 1;
            m_indicator.m_resultVar.m_str = "'" + text + "'";
        }
        else
        {
            String name = m_indicator.getText(var.m_parameters[0]);
            FCView view = m_xml.findView(name);
            if (name.length() == 0)
            {
                view = m_xml.getNative().getFocusedView();
            }
            String text = "";
            if (view != null)
            {
                FCGrid grid = (FCGrid) ((view instanceof FCGrid) ? view : null);
                if (grid != null)
                {
                    String propertyName = m_indicator.getText(var.m_parameters[1]);
                    if (propertyName.indexOf("cell.") != -1)
                    {
                        int rowIndex = -1;
                        if (m_indicator.getText(var.m_parameters[2]).equals("selectedrow"))
                        {
                            if (grid.getSelectedRows().size() > 0)
                            {
                                rowIndex = grid.getSelectedRows().get(0).getIndex();
                            }
                        }
                        else
                        {
                            rowIndex = (int)m_indicator.getValue(var.m_parameters[2]);
                        }
                        if (rowIndex != -1)
                        {
                            String colName = m_indicator.getText(var.m_parameters[3]);
                            FCGridRow row = grid.getRow(rowIndex);
                            if (row != null)
                            {
                                FCGridCell cell = row.getCell(colName);
                                String argType = "";
                                RefObject<String> refText = new RefObject<String>(text);
                                RefObject<String> refType = new RefObject<String>("");
                                cell.getAttribute(propertyName.replace("cell.", ""), refText, refType);
                                text = refText.argvalue;
                            }
                        }
                    }
                }
            }
            m_indicator.m_resultVar = new CVar();
            m_indicator.m_resultVar.m_type = 1;
            m_indicator.m_resultVar.m_str = "'" + text + "'";
        }
        return 0;
    }

    /*
    * 获取调用者
    */
    public double GETSENDER(CVariable var) {
        FaceCatScript fScript = (FaceCatScript)m_xml.getScript();
        String text = fScript.getSender();
        if (var.m_parameters == null || var.m_parameters.length == 0)
        {
            m_indicator.m_resultVar = new CVar();
            m_indicator.m_resultVar.m_type = 1;
            m_indicator.m_resultVar.m_str = "'" + text + "'";
        }
        else
        {
            CVariable newVar = new CVariable(m_indicator);
            newVar.m_expression = "'" + text + "'";
            m_indicator.setVariable(var.m_parameters[0], newVar);
        }
        return 0;
    }

    /*
    * 刷新界面
    */
    private double INVALIDATE(CVariable var) {
        if (m_xml != null) {
            int pLen = var.m_parameters != null ? var.m_parameters.length : 0;
            if (pLen == 0) {
                m_xml.getNative().invalidate();
            }
            else {
                FCView control = m_xml.findView(m_indicator.getText(var.m_parameters[0]));
                if (control != null) {
                    control.invalidate();
                }
            }
        }
        return 0;
    }

    /*
    * 设置属性
    */
    private double SETATTRIBUTE(CVariable var) {
        FaceCatScript script = (FaceCatScript)m_xml.getScript();
        String name = m_indicator.getText(var.m_parameters[0]);
        FCView view = m_xml.findView(name);
        if (var.m_parameters.length == 3)
        {
            String propertyName = m_indicator.getText(var.m_parameters[1]);
            String propertyValue = m_indicator.getText(var.m_parameters[2]);
            if (name.length() == 0)
            {
                if (m_xml.getNative().getFocusedView() != null)
                {
                    m_xml.getNative().getFocusedView().setAttribute(propertyName, propertyValue);
                }
            }
            else
            {
                script.setAttribute(name, propertyName, propertyValue);
            }
        }
        else
        {
            if (name.length() == 0)
            {
                view = m_xml.getNative().getFocusedView();
            }
            if (view != null)
            {
                FCGrid grid = (FCGrid) ((view instanceof FCGrid) ? view : null);
                if (grid != null)
                {
                    String propertyName = m_indicator.getText(var.m_parameters[1]);
                    if (propertyName.indexOf("cell.") != -1)
                    {
                        int rowIndex = -1;
                        if (m_indicator.getText(var.m_parameters[2]).equals("selectedrow"))
                        {
                            if (grid.getSelectedRows().size() > 0)
                            {
                                rowIndex = grid.getSelectedRows().get(0).getIndex();
                            }
                        }
                        else
                        {
                            rowIndex = (int)m_indicator.getValue(var.m_parameters[2]);
                        }
                        String colName = m_indicator.getText(var.m_parameters[3]);
                        FCGridRow row = grid.getRow(rowIndex);
                        if (row != null)
                        {
                            FCGridCell cell = row.getCell(colName);
                            cell.setAttribute(propertyName.replace("cell.", ""), m_indicator.getText(var.m_parameters[4]));
                        }
                    }
                }
            }
        }
        return 0;
    }

    /*
    * 开始秒表
    */
    private double STARTTIMER(CVariable var) {
        FCView control = m_xml.findView(m_indicator.getText(var.m_parameters[0]));
        control.startTimer((int)m_indicator.getValue(var.m_parameters[1]), (int)m_indicator.getValue(var.m_parameters[2]));
        return 0;
    }

    /*
    * 结束秒表
    */
    private double STOPTIMER(CVariable var) {
        FCView control = m_xml.findView(m_indicator.getText(var.m_parameters[0]));
        control.stopTimer((int)m_indicator.getValue(var.m_parameters[1]));
        return 0;
    }

    /*
    * 更新布局
    */
    private double UPDATE(CVariable var) {
        if (m_xml != null) {
            int pLen = var.m_parameters != null ? var.m_parameters.length : 0;
            if (pLen == 0) {
                m_xml.getNative().update();
            }
            else {
                FCView control = m_xml.findView(m_indicator.getText(var.m_parameters[0]));
                if (control != null) {
                    control.update();
                }
            }
        }
        return 0;
    }
}